package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.filecenter.domain.po.tags.TagsPo;
import com.bwda.dsrs.filecenter.domain.condition.tags.TagsCondition;
import com.bwda.dsrs.filecenter.form.tags.TagsQueryForm;
import com.bwda.dsrs.filecenter.form.tags.TagsUpdateForm;
import com.bwda.dsrs.filecenter.form.tags.TagsCreateForm;
import com.bwda.dsrs.filecenter.vo.tags.TagsVo;
import com.bwda.dsrs.filecenter.service.TagsService;

import java.util.List;
import java.util.ArrayList;

import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang.math.RandomUtils;

@RestController
@RequestMapping("/b/tags")
@Api(value = "/b/tags", tags = "标签表")
public class TagsController extends BaseController {

    @Autowired
    private TagsService tagsService;



    @ApiOperation(value = "查询标签表列表(带分页)", notes = "查询标签表列表(带分页)", httpMethod = "GET")
    @RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<CentreCutPageResponse<TagsVo>> queryPageList(@ModelAttribute @Valid TagsQueryForm form) throws BusinessServiceException {
        TagsCondition condition = this.getConditionByQueryForm(form);
        List<TagsVo> voList = new ArrayList<>();
        int count = tagsService.queryCount(condition);
        if (count > 0) {
            List<TagsPo> poList = tagsService.queryList(condition);
            voList = CopyUtil.transfer(poList, TagsVo.class);
        }
        return getSuccessResult(getPageResponse(form, count, voList));
    }

    @ApiOperation(value = "新增标签表", notes = "新增标签表", httpMethod = "POST")
    @RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<TagsVo> add(@ModelAttribute @Valid TagsCreateForm form) throws BusinessServiceException {
        TagsPo po = CopyUtil.transfer(form, TagsPo.class);
        po.setTagId(tagsService.queryId());
        tagsService.insert(po);
        TagsVo vo = CopyUtil.transfer(po, TagsVo.class);
        return getSuccessResult(vo);
    }

    @ApiOperation(value = "修改标签表", notes = "修改标签表", httpMethod = "POST")
    @RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity update(@ModelAttribute @Valid TagsUpdateForm form) throws BusinessServiceException {
        TagsPo po = CopyUtil.transfer(form, TagsPo.class);
        tagsService.update(po);
        return getSuccessResult();
    }

    @ApiOperation(value = "删除标签表", notes = "删除标签表", httpMethod = "POST")
    @RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity delete(@ApiParam(value = "ID", required = true) @RequestParam Integer tagId) throws BusinessServiceException {
        tagsService.delete(tagId);
        return getSuccessResult();
    }

    /**
     * TagsQueryForm转换为TagsCondition
     *
     * @param form
     * @return
     */
    private TagsCondition getConditionByQueryForm(TagsQueryForm form) {
        TagsCondition condition = CopyUtil.transfer(form, TagsCondition.class);
        return condition;
    }

}